package com.microservice.wechat.controller;

import com.microservice.api.common.Result;
import com.microservice.api.wechat.WechatDraftServiceClient;
import com.microservice.api.wechat.dto.ArticleDTO;
import com.microservice.api.wechat.dto.ArticleEndMd;
import com.microservice.api.wechat.dto.DraftAddResponse;
import com.microservice.common.utils.WechatTokenUtil;
import com.microservice.wechat.entity.dto.DraftAddRequest;
import com.microservice.wechat.service.ArticleService;
import com.microservice.wechat.service.WechatDraftService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 微信草稿箱服务API实现
 * 实现common-api中定义的WechatDraftServiceApi接口
 * 
 * @author wechat-service
 */
@RestController
@RequestMapping("/wechat/draft")
public class WechatDraftController implements WechatDraftServiceClient {
    
    @Autowired
    private WechatDraftService wechatDraftService;
    
    @Autowired
    private WechatTokenUtil wechatTokenUtil;
    
    @Override
    public Result<DraftAddResponse> addDraft(ArticleDTO articleDTO) {
        try {
            // 获取随机Access Token
            Map<String, String> accountMap = wechatTokenUtil.getRandomAccessToken();
            String accessToken = accountMap.get("token");
            
            // 构建草稿请求
            DraftAddRequest request = new DraftAddRequest();
            List<com.microservice.wechat.entity.dto.ArticleDTO> dtoList = new ArrayList<>();

            com.microservice.wechat.entity.dto.ArticleDTO article = new com.microservice.wechat.entity.dto.ArticleDTO();
            article.setArticle_type("article_type");
            article.setTitle(articleDTO.getTitle());
            article.setAuthor(articleDTO.getAuthor() != null ? articleDTO.getAuthor() : "CODE");
            article.setDigest(articleDTO.getDigest() != null ? articleDTO.getDigest() : "");
            article.setContent(articleDTO.getContent());
            article.setContent_source_url(articleDTO.getContentSourceUrl() != null ? articleDTO.getContentSourceUrl() : "");
          //  article.setThumb_media_id(wechatTokenUtil.getCoverId(articleDTO.getAppId()));
            article.setNeed_open_comment(articleDTO.getNeedOpenComment() != null ? articleDTO.getNeedOpenComment() : 1);
            article.setOnly_fans_can_comment(articleDTO.getOnlyFansCanComment() != null ? articleDTO.getOnlyFansCanComment() : 0);
            ArticleEndMd articleEndMd= wechatDraftService.getArticleEndMd(accountMap.get("appId"));
            if(articleEndMd!=null){
                article.setContent(article.getContent().concat(articleEndMd.getContent()));
            }
            dtoList.add(article);
            request.setArticles(dtoList);
            
            // 调用微信API添加草稿
            com.microservice.wechat.entity.dto.DraftAddResponse wechatResponse = wechatDraftService.addDraft(request, accessToken);
            
            // 转换为API的DraftAddResponse
            DraftAddResponse response = new DraftAddResponse();
            BeanUtils.copyProperties(wechatResponse, response);
            
            // 检查微信API返回的错误码
            if (response.getMedia_id()!=null) {
                return Result.success(response);
            } else {
                String errorMsg = response.getErrmsg() != null ? response.getErrmsg() : "新建草稿失败";
                return Result.error(errorMsg);
            }
        } catch (Exception e) {
            return Result.error("新建草稿失败: " + e.getMessage());
        }
    }
}
